package com.jdjc.auth.application.controller;

import com.google.common.base.Preconditions;
import com.jdjc.auth.application.convert.AuthRoleDTOConverter;
import com.jdjc.auth.application.dto.AuthRoleDTO;
import com.jdjc.auth.entity.Result;
import com.jdjc.auth.domin.entity.AuthRoleBO;
import com.jdjc.auth.domin.service.AuthRoleDominService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色Controller
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDominService authRoleDominService;

    /**
     * 新增角色
     * @param authRoleDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" RoleController：add:dto:{}", authRoleDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkNotNull(authRoleDTO.getRoleName(),"角色名称RoleName不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(),"角色唯一标识RoleKey不能为空");
            //自定义类型装换
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            if (log.isInfoEnabled()) {
                log.info(" RoleController：add:bo:{}", authRoleBO);
            }
            //注册
            Boolean result = authRoleDominService.add(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RoleController:add:error:{}", e.getMessage(), e);
            return Result.fail("新增角色失败");
        }

    }
    /**
     * 修改角色
     * @param authRoleDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" RoleController：update:dto:{}", authRoleDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");
            //自定义类型装换
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            if (log.isInfoEnabled()) {
                log.info(" RoleController：update:bo:{}", authRoleBO);
            }
            Boolean result = authRoleDominService.update(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RoleController:add:error:{}", e.getMessage(), e);
            return Result.fail("修改角色失败");
        }

    }
    /**
     * 修改角色
     * @param authRoleDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" RoleController:delete:dto:{}", authRoleDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");
            //自定义类型装换
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            if (log.isInfoEnabled()) {
                log.info(" RoleController：delete:bo:{}", authRoleBO);
            }
            Boolean result = authRoleDominService.delete(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RoleController:add:error:{}", e.getMessage(), e);
            return Result.fail("修改角色失败");
        }

    }
}
